﻿@using SelfMadeMillionaire.Core.Entities
@model SelfMadeMillionaire.Web.Infrastructure.ViewModels.Account.PortfoliosViewModel
<div style="margin-top: 20px;">
	<div>
        @Html.HiddenFor(m => m.SelectedPortfolioId)
        
        <div class="content_block_title">
            <h5 style="text-transform:none !important">
                My Watch Lists
                <a id="btnAddNewPortfolio" href="javascript:void(0);" data-id="-1" style="display:inline; float:right; text-decoration:underline !important" class="edit-portfolio">Add New</a>
            </h5>
            
            <div class="title_bottom_arrow">
					<img src="@Url.Content("~/Content/images/down_arrow.png")" alt="" />
			</div>
        </div>
        
        @if (Model.Portfolios.Any())
        {
        <div class="form-group" style="padding-left:20px;padding-top:20px">
        @foreach (UserPortfolio item in Model.Portfolios)
        {
            
            <a href="javascript:void(0);" class="link-portfolio" data-id="@item.ID">@item.PortfolioName</a>
            <a href="javascript:void(0);" onclick="if (confirm('Are you sure you want to delete this watch list?')) { DeleteUserPortfolio(@(item.ID)) }">
                <img src="@Url.Content("~/Content/Images/delete.png")" alt="" title="Delete watch list" border="0" />
            </a>
            <span style="margin-left: 10px;"></span>
            
        }
        </div>
        }

        @if (Model.Portfolios.Any() && (Model.SelectedPortfolioId > 0))
        {
        <div class="content_block_title">
            <h5 style="text-transform:none !important">@Model.SelectedPortfolioName
            
            @if (Model.SelectedPortfolioId > 0)
            {
                <a id="btnEditPortfolio" href="javascript:void(0);" data-id="@Model.SelectedPortfolioId" style="display:inline; float:right; text-decoration:underline !important" class="edit-portfolio">Rename</a>		
            }
            else
            {
                <a id="btnSaveNewPortfolio" href="javascript:void(0);" data-id="@Model.SelectedPortfolioId" style="display:inline; float:right; text-decoration:underline !important" class="edit-portfolio">Save</a>		
            }
            </h5>

            <div class="title_bottom_arrow">
					<img src="@Url.Content("~/Content/images/down_arrow.png")" alt="" />
			</div>
        </div>

        <div class="row">
            <div class="col-sm-4" style="padding-right:0px !important">
					    @Html.TextBoxFor(m => m.NameCompany, new { @class = "form-control", placeholder = "Enter Symbol" })
			</div>
            <div class="col-sm-2">
					    <button type="button" id="btnFindCompany" class="btn btn-info">Add</button>
			</div>
            <button type="button" id="btnRefreshSymbol" class="btn btn-info" style="display:inline; float:right; margin-right:15px" onclick="RefreshSymbols()">Refresh</button>
        </div>

        <div class="table_container">
            <div id="divCompanyInfoList">
		        @Html.Partial("CompanyInfoList", Model.InfoList)
            </div>
		</div>

        <div class="content_block_title">
            <h5 style="text-transform:none !important">
                Options
            </h5>
            <div class="title_bottom_arrow">
                <img src="@Url.Content("~/Content/images/down_arrow.png")" alt="" />
			</div>
        </div>

        @Html.HiddenFor(model => model.SymbolsList)
        @Html.HiddenFor(model => model.SortOrderSymbolOptionsHystory)
        <div class="form-horizontal">
        <div class="row">
                    <div class="col-sm-4">
				        <div class="form-group">
					        @Html.LabelFor(model => model.DaysInTradeFilter, new { @class = "col-sm-8 control-label custom_label" })
					        <div class="col-sm-4">
						        @Html.TextBoxFor(m => m.DaysInTradeFilter, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.DaysInTradeFilter)
					        </div>
				        </div>
			        </div>
                    <div class="col-sm-5">
				        <div class="form-group">
					        @Html.LabelFor(model => model.MinDiscountFilter, new { @class = "col-sm-8 control-label custom_label" })
					        <div class="col-sm-4">
						        @Html.TextBoxFor(m => m.MinDiscountFilter, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.MinDiscountFilter)
					        </div>
				        </div>
			        </div>
		</div>
                
        <div class="row">
                    <div class="col-sm-4">
				        <div class="form-group">
					        @Html.LabelFor(model => model.MinAnnualReturnFieldFilter, new { @class = "col-sm-8 control-label custom_label" })
					        <div class="col-sm-4">
						        @Html.DropDownListFor(m => m.MinAnnualReturnFieldFilter, Model.MinAnnualReturnFieldList, new { @class = "form-control" })
					        </div>
				        </div>
			        </div>
                    <div class="col-sm-1">
                        <div class="col-sm-12">
						    @Html.TextBoxFor(m => m.MinAnnualReturnFilter, new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.MinAnnualReturnFilter)
                        </div>
                    </div>
		</div>
                
        <div class="row">
                    <div class="col-sm-4">
				        <div class="form-group">
					        @Html.LabelFor(model => model.MinProbabilityFilter, new { @class = "col-sm-8 control-label custom_label" })
					        <div class="col-sm-4">
						        @Html.TextBoxFor(m => m.MinProbabilityFilter, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.MinProbabilityFilter)
					        </div>
				        </div>
			        </div>
                    <div class="col-sm-2">
                        <label class="radio-inline custom_check_radiobox">
                            @Html.RadioButtonFor(m => m.IsCallsFilter, true, new { name = "IsCallFilter", id = "IsCallsFilter" }) Calls
                        </label>
                        <label class="radio-inline custom_check_radiobox">
                            @Html.RadioButtonFor(m => m.IsCallsFilter, false, new { name = "IsCallFilter" }) Puts
                        </label>
                        
                    </div>

                    <div class="col-sm-2">
        		        <button type="button" id="btnFindHistory" class="btn btn-default">Search</button>
			        </div>
        </div>
        </div>

        <div class="page-header" style="margin-top:10px;">
            <b><span id="SymbolsOptionsHistoryHeader">@(Model.IsCallsFilter?"Calls":"Puts")</span></b>
        </div>
        <div class="table_container">
            <div id="divSymbolOptionsHistoryList">
		        @Html.Partial("SymbolOptionsHistoryList", Model.FilteredHystory)
            </div>
		</div>
        }
	</div>
</div>
<div id="userPortfolioDialog" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="edit-dialog-label" aria-hidden="true"></div>

<script type="text/javascript">
    function RefreshSymbols() {
        TestSession();

        var infoUrl = '@Url.Action("GetSymbols", new { id = Model.SelectedPortfolioId })';
        $('#divCompanyInfoList').load(infoUrl, function () {
        });
    }

    $(".edit-portfolio").click(function () {
        TestSession();

        $.ajax({
            url: '@Url.Action("PortfolioDialog", "Profile")',
            data: { id: $(this).attr('data-id') },
            cache: false,
            success: function (result) {
                $("#userPortfolioDialog").html(result);
                $("#userPortfolioDialog").modal();
            }
        });

    return false;
    });

    $(".link-portfolio").click(function () {
        var id = $(this).attr('data-id');
        $("#SelectedPortfolioId").val(id);
        LoadUserPortfolios();
        return false;
    });

    function DeleteUserPortfolio(id) {
        TestSession();

        $.ajax({
            url: "@Url.Action("DeletePortfolio")",
            type: "POST",
            cache: false,
            data: {
                id: id
            },
            success: function (result) {
                LoadUserPortfolios();
            }
        });
    }

    $("#btnFindCompany").click(function (e) {
        TestSession();

        $.ajax({
            url: "@Url.Action("GetSymbolData")",
            type: "POST",
            cache: false,
            data: {
                SelectedPortfolioId: $("#SelectedPortfolioId").val(), 
                NameCompany: $("#NameCompany").val()
            },
            success: function (result) {
                if (result.success) {
                    LoadUserPortfolios();
                }
                else {
                    alert(result.message);
                }
            }
        });
        
    });

    $("#btnFindHistory").click(function (e) {
        TestSession();

        var isCallsFilter = $('#IsCallsFilter').is(':checked');

        if (isCallsFilter)
            $('#SymbolsOptionsHistoryHeader').text('Calls');
        else
            $('#SymbolsOptionsHistoryHeader').text('Puts');

        $.get("@Url.Action("GetSymbolsOptionsHystory")",
                    {
                        IsCallsFilter: isCallsFilter,
                        DaysInTradeFilter: $("#@Html.IdFor(m => m.DaysInTradeFilter)").val(),
                        MinDiscountFilter: $("#@Html.IdFor(m => m.MinDiscountFilter)").val(),
                        MinAnnualReturnFieldFilter: $("#@Html.IdFor(m => m.MinAnnualReturnFieldFilter)").val(),
                        MinAnnualReturnFilter: $("#@Html.IdFor(m => m.MinAnnualReturnFilter)").val(),
                        MinProbabilityFilter: $("#@Html.IdFor(m => m.MinProbabilityFilter)").val(),
                        SortOrder: $("#@Html.IdFor(m => m.SortOrderSymbolOptionsHystory)").val(),
                        SymbolsList: $("#@Html.IdFor(m => m.SymbolsList)").val()
                    },
                    function (result) {
                        $("#divSymbolOptionsHistoryList").html(result);
                    });
    });
</script>
